//
//  AppDelegate.swift
//  微博
//
//  Created by 众力金融 on 2017/1/5.
//  Copyright © 2017年 众力金融. All rights reserved.
//

import UIKit
import CoreData
import UserNotifications
import SVProgressHUD
import AFNetworking

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

      /**
     微信小程序分享：
     微信小程序认证指引    http://t.cn/RI1RMEO
     微信小程序接入指南    http://t.cn/RI1RKRi
     微信小程序开发指南    http://t.cn/RI1RYEe
     微信小程序平台运营规范    http://t.cn/RI1R8tO
     个人进行申请开发者权限教程    http://t.cn/RI1Evtd
     小程序接入流程，主体验证    http://t.cn/RI1EyTO
     手机预览小程序demo流程    http://t.cn/RI1E5Am
     微信小程序支付文档    http://t.cn/RI1EIvV
     开发之间互相分享小程序教程    http://t.cn/RI1EJgl
     微信小程序服务类目范围
      */
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        window = UIWindow()
        //window?.backgroundColor = #colorLiteral(red: 1, green: 0.9336739182, blue: 0.3085899949, alpha: 1)
        window?.rootViewController = HRMainTabBarViewController()
        window?.makeKeyAndVisible()
         loadAppInfo()
        //应用额外的信息
        serupAddtions()
        return true
    }

    
}
//MARK: 设置应用程序额外的信息
extension AppDelegate {
    
    fileprivate func serupAddtions() {
        //1.设置SVP最小时间
        SVProgressHUD.setMinimumDismissTimeInterval(2)
        //设置网络加载指示器
        AFNetworkActivityIndicatorManager.shared().isEnabled = true
        //设置用户授权
        //Unnecessary check for 'iOS'; enclosing scope ensures guard will always be true
        if #available(iOS 10.0, *) {
            UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge, .carPlay, .sound]) { (success, error) in
                
            }
        } else {
            //取得用户的的授权显示通知[上方的提示条/声音/BadgeNumber]
            let notifySettings = UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
            UIApplication.shared.registerUserNotificationSettings(notifySettings)
        }
    }
}


//MARK: - 从服务器加载应用程序信息
extension AppDelegate {
    
    //1.模拟异步
    fileprivate func loadAppInfo() {
        //1.模拟异步
        DispatchQueue.global().async {
            
            DispatchQueue.main.asyncAfter(deadline: DispatchTime.now()) {
                //1.url
                let url = Bundle.main.url(forResource: "demo.json", withExtension: nil)
                //2.data
                let data = NSData(contentsOf: url!)
                //3.写入磁盘
                let docDir = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
                let jsonpath = (docDir as NSString).appendingPathComponent("demo.json")
                data?.write(toFile: jsonpath, atomically: true)
                print("应用程序加载完毕 \(jsonpath)")
            }
            
        }
    }
}

